﻿Public Class ProductDetail1
    Inherits CoreClass

    Public currUrl As String = String.Empty

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not Page.IsPostBack Then
                getPro()
                getOtherPro()
                'cmFb.InnerHtml = "<div class=""fb-comments"" data-href=""" & Request.Url.ToString & """ data-width=""570"" data-num-posts=""10""></div>"
            End If
        Catch ex As Exception

        End Try
        'lblcate.Text = idPro.ToString
    End Sub

    ' Get dữ liệu của Product fill vào form
    Private Sub getPro()
        Try
            Dim idPro As Integer = 0
            idPro = Integer.Parse(Request.QueryString("idPro"))
            If idPro <= 0 Then
                Response.Redirect("/")
                Exit Sub
            End If
            Dim imgAvatar As String = ""
            Dim proName As String = ""
            ' Khai bao bien cho slide
            Dim strGallery As String = ""
            Dim strPaging As String = ""

            sql = "spo_ProductGetByID " & objFunction.ValueCheck(idPro)
            ' SQL trên return lại 1 result set theo thứ tự: 0-Danh sách gallery, 1-Thong tin chi tiet san pham
            Dim dts As DataSet = objDataRead.ExeDataset(sql)

            ' Kiem tra va hien thi thong tin chi tiet san pham
            If dts.Tables(1).Rows.Count > 0 Then
                detailTitle.InnerHtml = dts.Tables(1).Rows(0)("ProName").ToString()
                Page.Title = dts.Tables(1).Rows(0)("ProName").ToString()
                proName = dts.Tables(1).Rows(0)("ProName").ToString()
                Dim d1 As Date = Date.Parse(dts.Tables(1).Rows(0)("ProDate").ToString())
                Dim strSaveFolder As String = "/EditorData/image" & Format(d1, "/yyyy/MM/dd/")
                imgAvatar = strSaveFolder & dts.Tables(1).Rows(0)("ProImage").ToString()
                detailContent.InnerHtml = dts.Tables(1).Rows(0)("ProDetail").ToString()
                lblNewPrice.Text = dts.Tables(1).Rows(0)("ProNewPrice").ToString()

                'Cart.OnClientClick = "addCart(1000);"
                'Cart.CommandArgument = dts.Tables(1).Rows(0)("ProID").ToString()
                If dts.Tables(1).Rows(0)("ProPromotion").ToString() = "" Then
                    detailPromo.Visible = False
                Else
                    detailPromoContent.InnerHtml = dts.Tables(1).Rows(0)("ProPromotion").ToString()
                End If

                If Integer.Parse(dts.Tables(1).Rows(0)("ProIsStock").ToString()) = 0 Then
                    lblIsStock.Text = "Hết hàng"
                    lblIsStock.CssClass = "no"

                Else
                    lblIsStock.Text = "Còn hàng"
                    lblIsStock.CssClass = "yes"
                    divShowCart.InnerHtml = "<input type=""button"" Class=""btnAddToCart"" onclick=""addCart(" & idPro & ", '" & dts.Tables(1).Rows(0)("ProName").ToString() & "', '" & imgAvatar.Replace("/", "|") & "', " & dts.Tables(1).Rows(0)("ProNewPrice").ToString() & ");"" />"
                End If
                descContent.InnerHtml = dts.Tables(1).Rows(0)("ProDesc").ToString()
                ' Gán ảnh sản phẩm vào phần tử đầu tiên của gallery
                strGallery &= "<a href='" & imgAvatar & "'><table class='tblImgPro'><tr><td><img src='" & imgAvatar & "' style='max-width:331px;max-height: 245px;' alt='" & proName & "'></td></tr></table></a>"
                strPaging &= "<li><a href='#'><table class='tblImgProIco'><tr><td><img src='" & imgAvatar & "' style='max-width:53px;max-height: 53px;' alt='" & proName & "'></td></tr></table></a></li>"

                ' Set facebook info
                objFunction.Set_Page_FB(Page, HttpContext.Current.Request.Url.AbsoluteUri, proName, objConfig.Domain & imgAvatar, dts.Tables(1).Rows(0)("ProDesc").ToString())


            Else ' Neu ID goi ra k ton tai thi redirect ve trang chu
                Response.Redirect("/")
                Exit Sub
            End If

            ' Kiem tra va hien thi Gallery
            If dts.Tables(0).Rows.Count > 0 Then
                For i As Integer = 0 To (dts.Tables(0).Rows.Count - 1)
                    strGallery &= "<a href='\" & dts.Tables(0).Rows(i)("GalleryImg").ToString() & "'><table class='tblImgPro'><tr><td><img src='\" & dts.Tables(0).Rows(i)("GalleryImg").ToString() & "' style='max-width:331px;max-height: 245px;' alt='" & proName & "'></td></tr></table></a>"
                    strPaging &= "<li><a href='#'><table class='tblImgProIco'><tr><td><img src='\" & dts.Tables(0).Rows(i)("GalleryImg").ToString() & "' style='max-width:53px;max-height: 53px;' alt='" & proName & "'></td></tr></table></a></li>"
                Next
            End If
            ' Show du lieu ra ngoai
            gallery.InnerHtml = strGallery
            pagination.InnerHtml = strPaging
        Catch ex As Exception
            If objConfig.Developer.Equals("1") Then
                objAlert.ErrorBox(Page, "File: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.DeclaringType.Name & _
                                  " --- Function: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.Name & _
                                  " --- Mess: " & ex.Message.Replace("'", """"))
            Else
                objAlert.ErrorBox(Page, "Lỗi kỹ thuật, hãy thông báo lại với Admin, xin cảm ơn.")
            End If
        End Try
    End Sub

    ' Lấy sản phẩm khác
    Public Sub getOtherPro()
        Try
            Dim idPro As Integer = 0
            idPro = Integer.Parse(Request.QueryString("idPro"))
            '@SubCatID nvarchar(5), --> Neu = 0 la lay tat ca tin 
            '@NotID nvarchar(50) --> Truyen vao ID bai hien tai de check trung
            '@No nvarchar(5) --> so luong tin hien thi
            sql = "spo_ProductGetListOther "
            sql &= objFunction.FieldCheck(0) & ", "
            sql &= objFunction.FieldCheck(idPro) & ", "
            sql &= objFunction.FieldCheck(6)

            Dim dts As DataSet = objDataRead.ExeDataset(sql)
            ' Đưa dữ liệu của bảng 0 vào Repeater
            rpItems.DataSource = dts.Tables(0)
            rpItems.DataBind()
            rpItems.Dispose()

        Catch ex As Exception
            If objConfig.Developer.Equals("1") Then
                objAlert.ErrorBox(Page, "File: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.DeclaringType.Name & _
                                  " --- Function: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.Name & _
                                  " --- Mess: " & ex.Message.Replace("'", """"))
            Else
                objAlert.ErrorBox(Page, "Lỗi kỹ thuật, hãy thông báo lại với Admin, xin cảm ơn.")
            End If
        End Try
    End Sub

    ' Build link đến chi tiết sản phẩm dựa trên tên danh mục, tên sp, id sp
    Public Function buildLinkDetailPro(ByVal cateName As String, ByVal proName As String, ByVal proId As String) As String
        'Return "/san-pham/" & cateName & "/" & proName & "-" & proId
        Return "/ProductDetail.aspx?idPro=" & proId
    End Function

    ' Build src image
    Public Function buildImage(ByVal strDate As String, ByVal strImage As String) As String
        Return objFunction.buildImageByDate(strDate, strImage)
    End Function

    ' Build src image CHO AJAX, REPLACE / thanh | de dua len URL
    Public Function buildImageAJAX(ByVal strDate As String, ByVal strImage As String) As String
        Return objFunction.buildImageByDate(strDate, strImage).Replace("/", "|")
    End Function

    ' Build ten san pham CHO AJAX, bo het cac ky tu gay loi cho java
    Public Function buildProductNameAJAX(ByVal strName As String) As String
        Return strName.Replace("'", "").Replace("""", "").Replace("&", "")
    End Function

    ' Built button add to card
    Public Function buildButtonAjax(ByVal id As String, ByVal name As String, ByVal strdate As String, ByVal img As String, ByVal price As String, ByVal stock As String)
        '(Eval("ProID"), Eval("ProName"), Eval("ProDate"), Eval("ProImage2"), Eval("ProNewPrice"))%>
        Dim strReturn As String = ""
        Try
            Dim isStock As Integer = 0
            isStock = Integer.Parse(stock)
            If (isStock = 1) Then
                strReturn = "<input type=""button"" Class=""iconAddCart"" onclick=""addCart(" & id & ", '" & buildProductNameAJAX(name) & "', '" & buildImageAJAX(strdate, img) & "', " & price & ");"" />"
            End If
        Catch ex As Exception

        End Try
        Return strReturn
    End Function

End Class